﻿using System;
using fastCSharp;

namespace diantou.web.topic
{
    /// <summary>
    /// 话题标识或者话题名称
    /// </summary>
    internal struct idName
    {
        /// <summary>
        /// 话题标识
        /// </summary>
        public int Id;
        /// <summary>
        /// 话题名称
        /// </summary>
        public string Name;
        /// <summary>
        /// 是否存在数据
        /// </summary>
        public bool IsValue
        {
            get { return Id != 0 || Name != null; }
        }
        /// <summary>
        /// 对象转换成JSON字符串
        /// </summary>
        /// <param name="toJsoner">对象转换成JSON字符串</param>
        /// <param name="value">参数</param>
        [fastCSharp.emit.jsonSerialize.custom]
        private static void toJson(fastCSharp.emit.jsonSerializer toJsoner, idName value)
        {
            fastCSharp.log.Error.Throw(fastCSharp.log.exceptionType.ErrorOperation);
        }
        /// <summary>
        /// 对象转换成JSON字符串
        /// </summary>
        /// <param name="parser">Json解析器</param>
        /// <param name="value">参数</param>
        [fastCSharp.emit.jsonParse.custom]
        private static void parseJson(fastCSharp.emit.jsonParser parser, ref idName value)
        {
            char currentChar = parser.CurrentChar;
            if (currentChar == '"')
            {
                subString name = default(subString);
                parser.Parse(ref name);
                if (parser.State == fastCSharp.emit.jsonParser.parseState.Success)
                {
                    if (name.Length != 0)
                    {
                        if ((uint)(name[0] - '1') <= 9)
                        {
                            if (int.TryParse(name, out value.Id)) return;
                        }
                        else if (name[0] == '_')
                        {
                            if (name.Length != 1)
                            {
                                value.Name = name.Substring(1);
                                return;
                            }
                        }
                        else
                        {
                            value.Name = name;
                            return;
                        }
                    }
                    parser.Error(fastCSharp.emit.jsonParser.parseState.NotNumber);
                }
            }
            else if ((uint)(currentChar - '1') <= 9) parser.Parse(ref value.Id);
            else
            {
                subString name = default(subString);
                parser.EndToSubString(ref name);
                if (name.Length > 0)
                {
                    if (name[0] == '_')
                    {
                        if (name.Length != 1)
                        {
                            value.Name = name.Substring(1);
                            return;
                        }
                    }
                    else
                    {
                        value.Name = name;
                        return;
                    }
                }
                parser.Error(fastCSharp.emit.jsonParser.parseState.NotString);
            }
        }
    }
}
